/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.EditPanes.Spatial;

import GUI.Components.BTextField;
import GUI.Components.ColorButton;
import GUI.Components.Float3Panel;
import GUI.Components.BSlider;
import GUI.EditPanes.EditTaskPane;
import General.CurrentData;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.math.Vector3f;
import other.Wizard;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import se.datadosen.component.RiverLayout;

/**
 *
 * @author David
 */
public class EmitterTaskPane extends EditTaskPane
{

    private ButtonGroup typeGroup = new ButtonGroup();
    private JRadioButton pointTypeRadioButton = new JRadioButton("Point", false);
    private JRadioButton triangleTypeRadioButton = new JRadioButton("Triangle", true);
    private BTextField maxParticlesField = new BTextField("int");
    private BTextField particlesPerSecondField = new BTextField("float");
    private BTextField startSizeField = new BTextField("float");
    private BTextField endSizeField = new BTextField("float");
    private BTextField highLifeField = new BTextField("float");
    private BTextField lowLifeField = new BTextField("float");
    private BSlider velocityVariationSlider = new BSlider(Float.class,0, 1, .25f);
    private Float3Panel gravityPanel = new Float3Panel(Vector3f.ZERO);
    private Float3Panel directionVelocityPanel = new Float3Panel(Vector3f.ZERO);
    private ColorButton startColorButton = new ColorButton(Color.white);
    private ColorButton endColorButton = new ColorButton(Color.white);
    private BTextField rotateSpeedField = new BTextField("float");
    private BTextField imgXField = new BTextField("int");
    private BTextField imgYField = new BTextField("int");
    private JCheckBox depthWriteCheckBox = new JCheckBox("Depth Write");

    /**
     *
     * @param particleEmitter
     */
    public EmitterTaskPane(final ParticleEmitter particleEmitter)
    {
        
        depthWriteCheckBox.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        particleEmitter.getMaterial().getAdditionalRenderState().setDepthWrite(depthWriteCheckBox.isSelected());
                        return null;
                    }
                });
            }
        });
        depthWriteCheckBox.setSelected(particleEmitter.getMaterial().getAdditionalRenderState().isDepthWrite());
        typeGroup.add(triangleTypeRadioButton);
        typeGroup.add(pointTypeRadioButton);

        if (particleEmitter.getMeshType().equals(ParticleMesh.Type.Point))
        {
            pointTypeRadioButton.setSelected(true);
        }
        velocityVariationSlider._setValue(particleEmitter.getParticleInfluencer().getVelocityVariation());
        maxParticlesField.setText("" + particleEmitter.getMaxNumParticles());
        particlesPerSecondField.setText("" + particleEmitter.getParticlesPerSec());
        startSizeField.setText("" + particleEmitter.getStartSize());
        endSizeField.setText("" + particleEmitter.getEndSize());
        highLifeField.setText("" + particleEmitter.getHighLife());
        lowLifeField.setText("" + particleEmitter.getLowLife());
        gravityPanel.setVector(particleEmitter.getGravity());
        startColorButton.setColor(Wizard.makeColor(particleEmitter.getStartColor()));
        endColorButton.setColor(Wizard.makeColor(particleEmitter.getEndColor()));
        rotateSpeedField.setText("" + particleEmitter.getRotateSpeed());
        imgXField.setText("" + particleEmitter.getImagesX());
        imgYField.setText("" + particleEmitter.getImagesY());
        directionVelocityPanel.setVector(particleEmitter.getParticleInfluencer().getInitialVelocity());
        taskPane.setLayout(new RiverLayout());
        velocityVariationSlider.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                particleEmitter.getParticleInfluencer().setVelocityVariation(velocityVariationSlider._getValue());
            }
        });
        applyButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                CurrentData.getEditorWindow().getB3DApplication().enqueue(new Callable<Void>()
                {
                    public Void call() throws Exception
                    {
                        particleEmitter.getMaterial().getAdditionalRenderState().setDepthWrite(depthWriteCheckBox.isSelected());
                        particleEmitter.setEndColor(Wizard.makeColorRGBA(endColorButton.getColor()));
                        particleEmitter.setEndSize(Float.parseFloat(endSizeField.getText()));
                        particleEmitter.setGravity(gravityPanel.getVector());
                        particleEmitter.setHighLife(Float.parseFloat(highLifeField.getText()));
                        particleEmitter.setImagesX(Integer.parseInt(imgXField.getText()));
                        particleEmitter.setImagesY(Integer.parseInt(imgYField.getText()));
                        particleEmitter.setLowLife(Float.parseFloat(lowLifeField.getText()));
                        particleEmitter.setStartColor(Wizard.makeColorRGBA(startColorButton.getColor()));
                        particleEmitter.getParticleInfluencer().setInitialVelocity(directionVelocityPanel.getVector());
                        particleEmitter.getParticleInfluencer().setVelocityVariation(velocityVariationSlider._getValue());
                        if (triangleTypeRadioButton.isSelected())
                        {
                            particleEmitter.setMeshType(ParticleMesh.Type.Triangle);
                        } else
                        {
                            particleEmitter.setMeshType(ParticleMesh.Type.Point);
                        }
                        particleEmitter.setNumParticles(Integer.parseInt(maxParticlesField.getText()));
                        particleEmitter.setParticlesPerSec(Float.parseFloat(particlesPerSecondField.getText()));
                        particleEmitter.setRotateSpeed(Float.parseFloat(rotateSpeedField.getText()));
                        particleEmitter.setStartSize(Float.parseFloat(startSizeField.getText()));
                        return null;
                    }
                });
            }
        });
        taskPane.add("left", new JLabel("Mesh Type:"));
        taskPane.add("tab hfill", pointTypeRadioButton);
        taskPane.add("br tab tab hfill", triangleTypeRadioButton);
        taskPane.add("br left", new JLabel("Max. Particles:"));
        taskPane.add("tab hfill", maxParticlesField);
        taskPane.add("br left", new JLabel("Particles per Sec.:"));
        taskPane.add("tab hfill", particlesPerSecondField);
        taskPane.add("br left", new JLabel("Velocity Variation:"));
        taskPane.add("tab hfill", velocityVariationSlider);
        taskPane.add("br left", new JLabel("Start Size:"));
        taskPane.add("tab hfill", startSizeField);
        taskPane.add("br left", new JLabel("End Size:"));
        taskPane.add("tab hfill", endSizeField);
        taskPane.add("br left", new JLabel("Start Color:"));
        taskPane.add("tab hfill", startColorButton);
        taskPane.add("br left", new JLabel("End Color:"));
        taskPane.add("tab hfill", endColorButton);
        taskPane.add("br left", new JLabel("High Life:"));
        taskPane.add("tab hfill", highLifeField);
        taskPane.add("br left", new JLabel("Low Life:"));
        taskPane.add("tab hfill", lowLifeField);
        taskPane.add("br left", new JLabel("Gravity:"));
        taskPane.add("tab hfill", gravityPanel);
        taskPane.add("br left", new JLabel("Direction Velocity:"));
        taskPane.add("tab hfill", directionVelocityPanel);
        taskPane.add("br left", new JLabel("Rotate Speed:"));
        taskPane.add("tab hfill", rotateSpeedField);
        taskPane.add("br left", new JLabel("Img X:"));
        taskPane.add("tab hfill", imgXField);
        taskPane.add("br left", new JLabel("Img Y:"));
        taskPane.add("tab hfill", imgYField);
        taskPane.add("br hfill", depthWriteCheckBox);
        taskPane.add("br right", applyButton);
        taskPane.setTitle("Particle Emitter");
        add(taskPane, BorderLayout.CENTER);
    }
}
